﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：DapperRepository.cs
// 功能描述：TongTongMall.Dapper
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using TongTongMall.Dapper.DapperExtensions;
using TongTongMall.Domain.PredicateForDapper;
using TongTongMall.Domain.Repositories.Entities;
using TongTongMall.Domain.Repositories.Repositories;

namespace TongTongMall.Dapper.Repository
{
    public class DapperRepositoryBase<TEntity, TPrimaryKey> : RepositoryBase<TEntity, TPrimaryKey>
            where TEntity : class, IEntity<TPrimaryKey>,new()
    {
        public IDatabase DB { get { return _dbContextProvider.GetDbContext(); } }
        private IDbContextProvider<IDatabase> _dbContextProvider { get; set; }
        public DapperRepositoryBase(IDbContextProvider<IDatabase> dbContextProvider)
        {
            _dbContextProvider = dbContextProvider;
        }

        public override int Count(object whereClip = null)
        {
            return DB.Count<TEntity>(whereClip);
        }

        public override async Task<int> CountAsync(object whereClip = null)
        {
            return await DB.CountAsync<TEntity>(whereClip);
        }

     
        public override bool Delete(object whereClip)
        {
            return DB.Delete<TEntity>(whereClip);
        }

        public override async Task<bool> DeleteAsync(object whereClip)
        {
            return await DB.DeleteAsync<TEntity>(whereClip);
        }

        public override TEntity FirstOrDefault(object whereClip = null, IList<ISort> orderClip = null, Expression<Func<TEntity, object>> selector = null)
        {
            return DB.GetList<TEntity>(whereClip, orderClip, selector).FirstOrDefault();
        }

        public override async Task<TEntity> FirstOrDefaultAsync(object whereClip = null, IList<ISort> orderClip = null, Expression<Func<TEntity, object>> selector = null)
        {
            var list = await DB.GetListAsync<TEntity>(whereClip, orderClip, selector);
            return list.FirstOrDefault();
        }

        public override List<TEntity> GetAllList(object whereClip = null, IList<ISort> orderClip = null, Expression<Func<TEntity, object>> selector = null)
        {
            return DB.GetList<TEntity>(whereClip, orderClip, selector).ToList();
        }

        public override async Task<List<TEntity>> GetAllListAsync(object whereClip = null, IList<ISort> orderClip = null, Expression<Func<TEntity, object>> selector = null)
        {
            var list = await DB.GetListAsync<TEntity>(whereClip, orderClip, selector);
            return list.ToList();
        }


        public override TPrimaryKey Insert(TEntity entity)
        {
            return DB.Insert<TEntity>(entity);
        }

        public override async Task<TPrimaryKey> InsertAsync(TEntity entity)
        {
            return await DB.InsertAsync<TEntity>(entity);
        }

        public override bool Update(object entity, object whereClip)
        {
            return DB.Update<TEntity>(entity, whereClip: whereClip);
        }

        public override Task<bool> UpdateAsync(object entity, object whereClip)
        {
            return DB.UpdateAsync<TEntity>(entity, whereClip: whereClip);
        }

        public override IEnumerable<TEntity> QueryPaged(int pageIndex, int pageSize, object whereClip = null, IList<ISort> orderClip = null, Expression<Func<TEntity, object>> selector = null)
        {
            return DB.QueryPaged<TEntity>(pageIndex, pageSize, whereClip, orderClip, selector);
        }

        public async override Task<IEnumerable<TEntity>> QueryPagedAsync(int pageIndex, int pageSize, object whereClip = null, IList<ISort> orderClip = null, Expression<Func<TEntity, object>> selector = null)
        {
            return await DB.QueryPagedAsync<TEntity>(pageIndex, pageSize, whereClip, orderClip, selector);
        }

        public override bool Update(TEntity entity)
        {
            return DB.Update<TEntity>(entity);
        }

        public override async Task<bool> UpdateAsync(TEntity entity)
        {
            return await DB.UpdateAsync<TEntity>(entity);
        }
    }
}
